<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'includes/connect.php';
require_once 'includes/function.php';

$id = $_REQUEST["id"];
$task = $_REQUEST["task"];
$card_id = $_REQUEST["card_id"];

$e1 = $_REQUEST["e1"];

$promise_date = $_REQUEST["promise_date"];
$reason = $_REQUEST["reason"];
$details = $_REQUEST["details"];
$budget_year = $_REQUEST["budget_year"];
$panngan2_id = $_REQUEST["panngan2_id"];
$panngan3_id = $_REQUEST["panngan3_id"];
$ex1_id = $_REQUEST["ex1_id"];
$ex2_id = $_REQUEST["ex2_id"];
$ex3_id = $_REQUEST["ex3_id"];
$advances = $_REQUEST["advances"];
$allowances = $_REQUEST["allowances"];
$rest_rate = $_REQUEST["rest_rate"];
$vehicle_rate = $_REQUEST["vehicle_rate"];
$other = $_REQUEST["other"];
$total = $_REQUEST["total"];

$cancel_date = $_REQUEST["cancel_date"];
$cancel_reason = $_REQUEST["cancel_reason"];

//$total = 56356;
//echo $task;



$db = new myDBS();

switch ($task) {
    case "new":

        $budget = get_budget($panngan2_id, $panngan3_id,  $ex1_id, $ex2_id, "", $budget_year);

        $budget_use = get_budget_used($panngan2_id, $panngan3_id, $ex1_id, $ex2_id, $ex3_id, $budget_year);

        $budget_remain = $budget - $budget_use;
        $budget_remain_this = $budget_remain - $total;

        $sql = "insert into tbl_promise_budget (promise_date , card_id1, card_id2, card_id3, reason , details , panngan2_id ,
                panngan3_id , ex1_id ,ex2_id  , ex3_id , advances ,  allowances  , rest_rate ,
                vehicle_rate ,other , total, remain ,status, budget_year
                , budget_remain, budget_remain_this ) "
                . "values ('$promise_date', '$e1[0]', '$e1[1]', '$e1[2]', '$reason', '$details', '$panngan2_id', '$panngan3_id', '$ex1_id', "
                . " '$ex2_id', '$ex3_id', '$advances', '$allowances', '$rest_rate', '$vehicle_rate', '$other', '$total', '$total', 'new', '$budget_year'"
                . ", '$budget_remain', '$budget_remain_this') ";
        $db->runSQL($sql) or die(mysql_error());
//        echo $sql;

        break;
    case "edit":
        $sql = "UPDATE tbl_promise_budget "
                . "SET promise_date = '$promise_date' "
                . ", card_id1 = '$e1[0]'"
                . ", card_id2 = '$e1[1]'"
                . ", card_id3 = '$e1[2]'"
                . ", reason = '$reason'"
                . ", details = '$details'"
                . ", panngan2_id = '$panngan2_id'"
                . ", panngan3_id = '$panngan3_id'"
                . ", ex1_id = '$ex1_id'"
                . ", ex2_id = '$ex2_id'"
                . ", ex3_id = '$ex3_id'"
                . ", advances = '$advances'"
                . ", allowances = '$allowances'"
                . ", rest_rate = '$rest_rate'"
                . ", vehicle_rate = '$vehicle_rate'"
                . ", other = '$other'"
                . ", total = '$total'"
                . ", remain = '$total' "
                . ", budget_year = '$budget_year' "
                . " WHERE id = '$id'";

        echo $sql;
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "cancel":
        $sql = "UPDATE tbl_promise_budget "
                . " SET cancel_date = '$cancel_date', "
                . " cancel_reason = '$cancel_reason', "
                . " status = 'cancel' "
                . " WHERE id = '$id'";
        //echo $sql;

        $db->runSQL($sql) or die(mysql_error());

        break;
}
//echo $task;
?>

